package com.winshare.saleAssistant.job;

import com.winshare.saleAssistant.service.JXZYZXService;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.TimeUnit;

@SuppressWarnings("DuplicatedCode")
@Slf4j
@Component
public class TaskService {

    @Resource
    JXZYZXService jxzyzxService;

    @XxlJob("QueryUserHandler")
    public ReturnT<String> executeQueryUser(String param) throws Exception {
        log.info("正在执行同步用户账户信息job.............");
        jxzyzxService.queryUser();
        return ReturnT.SUCCESS;
    }

    @XxlJob("QueryModulesHandler")
    public ReturnT<String> executeQueryModules(String param) throws Exception {
        log.info("正在执行同步功能信息job.............");
        jxzyzxService.queryModules();
        return ReturnT.SUCCESS;
    }

    @XxlJob("QueryLoginLogsHandler")
    public ReturnT<String> executeLoginLogs(String param) throws Exception {
        log.info("正在执行同步用户登录信息job.............");
        LocalDateTime yd = LocalDateTime.now().plusDays(-1);
        LocalDateTime bgTime = LocalDateTime.of(yd.toLocalDate(), LocalTime.MIN);
        LocalDateTime edTime = LocalDateTime.of(yd.toLocalDate(), LocalTime.MAX);
        String beginDate = bgTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        String endDate = edTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        jxzyzxService.queryLoginLogs(beginDate, endDate);
        return ReturnT.SUCCESS;
    }

    @XxlJob("QueryUseOperationLogsHandler")
    public ReturnT<String> executeUseOperationLogs(String param) throws Exception {
        log.info("正在执行同步用户操作信息job.............");
        LocalDateTime yd = LocalDateTime.now().plusDays(-1);
        LocalDateTime bgTime = LocalDateTime.of(yd.toLocalDate(), LocalTime.MIN);
        LocalDateTime edTime = LocalDateTime.of(yd.toLocalDate(), LocalTime.MAX);
        String beginDate = bgTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        String endDate = edTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        jxzyzxService.queryUseOperationLogs(beginDate, endDate);
        return ReturnT.SUCCESS;
    }
}
